package com.woniu.micropropertyserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.microcommonsserver.entity.OwnerInfoEntity;
import com.woniu.microcommonsserver.entity.RenterInfoEntity;
import com.woniu.micropropertyserver.mapper.OwnerMapper;
import com.woniu.micropropertyserver.mapper.RenterInfoMapper;
import com.woniu.micropropertyserver.service.IRenterInfoService;
import com.woniu.micropropertyserver.util.PageRest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：bb
 * @date ：Created in 2021/1/23 0023
 * @description：
 * @modified By：
 * @version: $
 */
@Transactional(readOnly = true)
@Service
public class RenterInfoServiceImpl implements IRenterInfoService {

    @Resource
    private RenterInfoMapper renterInfoMapper;
    @Resource
    private OwnerMapper ownerMapper;

    @Override
    public List<RenterInfoEntity> findall() {
        return renterInfoMapper.findall();
    }

    @Override
    public RenterInfoEntity findById(Long id) {
        return renterInfoMapper.findById(id);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    public int updateInfo(RenterInfoEntity renterInfo) {
        return renterInfoMapper.updateInfo(renterInfo);
    }

    @Transactional(isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    @Override
    public int delInfo(Long id) {
        return renterInfoMapper.delInfo(id);
    }

    @Transactional(isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    @Override
    public Long saveInfo(RenterInfoEntity renterInfo) {
        long id=0l;

        if(renterInfo.getOwnerInfo().getName()!=null && renterInfo.getOwnerInfo().getHouseNum()!=null){
            OwnerInfoEntity owner =  ownerMapper.findByNameAndHouse(renterInfo.getOwnerInfo().getName(),renterInfo.getOwnerInfo().getHouseNum());

            if(owner!=null ){
                id=owner.getId();
            }
            renterInfo.setFkOwnerId(id);

        }
        return renterInfoMapper.saveInfo(renterInfo);
    }

    @Override
    public PageInfo<RenterInfoEntity> findAllByParams(PageRest pageRest,RenterInfoEntity renterInfo) {
        System.out.println(renterInfo);
        return PageHelper.startPage(pageRest.getPageNum(),pageRest.getPageSize()).doSelectPageInfo(()->renterInfoMapper.findAllByParams(pageRest,renterInfo));
    }

    @Override
    public List<RenterInfoEntity> findAll() {
        return null;
    }

    @Override
    public RenterInfoEntity findByCard(String id) {
        return renterInfoMapper.findByCard(id);
    }

}
